package com.honeycomb.db.dao.hibernate.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.honeycomb.db.dao.BaseHibernateDao;
import com.honeycomb.db.dao.hibernate.ITaginfoDao;
import com.honeycomb.db.entity.TagInfo;
import com.honeycomb.utils.HoneyCombColumnIndicator;
import com.honeycomb.utils.HoneyCombTools;
import com.honeycomb.utils.SystemConstants;

public class TaginfoDaoImpl extends BaseHibernateDao<TagInfo, String> implements ITaginfoDao {

	@Override
	public TagInfo findByTagName(final String tagName) {
		System.out.println("Hello - TaginfoDaoImpl - findByTagName...");
		List<TagInfo> resultTagList = null;
		try {
			List list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = session.createQuery("from TagInfo ti where ti.tagName =:tn");
					query.setString("tn", tagName);
					query.setMaxResults(1);
					return query.list();
				}
			});
			resultTagList = list;
			if (resultTagList.size() > 0) {
				return resultTagList.get(0);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<TagInfo> findHotTagList() {
		System.out.println("Hello - TaginfoDaoImpl - findHotTagList...");
		List<TagInfo> tagInfoList = null;
		try {
			List list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					StringBuffer sb = new StringBuffer();
					sb.append("from TagInfo ti order by ti.tagSeq desc");
					Query query = session.createQuery(sb.toString());
					query.setMaxResults(SystemConstants.TAG_QUERY_HOT_LIST_DEFAULT_CNT);
					return query.list();
				}
			});
			tagInfoList = list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		if(tagInfoList != null) {
			return tagInfoList;
		}else {
			return null;
		}
	}
}














